<script setup lang="ts">
	import {ref,reactive,getCurrentInstance} from "vue";
	import { onShow, onReachBottom} from '@dcloudio/uni-app';
	import {useRouter} from 'uni-mini-router'
	import { userStore } from '@/pinia';
	const {proxy} = getCurrentInstance()
	const userStoreInfo = userStore();
	let tabList = reactive([{
                    name: '全部',
                }, {
                    name: '待接单',
                }, {
                    name: '待服务'
                }, {
                    name: '待验收'
                }, {
                    name: '已完成'
                }])
	let obj = reactive({
		pageNo:1,
		current:0,
		reasonId:'',
		currentObj:{}
	})
	let detailList = ref([])
	let cancelOrderReason = ref([])
	
	const router = useRouter()
		
	let pageNo = 1
	const tabClick = (e) => {
		console.log(e)
		detailList.value = []
		obj.current = e.index
		obj.pageNo = 1;
		getEmployerList()
	}
	
	const getEmployerList = ()=>{
		const par  ={
			pageNo:obj.pageNo,
			pageSize:10,
			employerMemberId:userStoreInfo.userInfo.id,
			orderStatus:obj.current-1
		}
		proxy.$req('GET', proxy.$api.employerList,par).then((res) => {
			if(res.code === 200){
				obj.total = res.result.total
				const  statusList = ['未支付','已支付','待接单','待服务','待验收','已完成','已取消']
				detailList.value = detailList.value.concat(res.result.records.map(e=>{
					return {
						title:e.employmentType==0?`日雇佣·${e.num}日`:`小时雇佣·${e.num}小时`,
						right:statusList[e.status+2],
						leixing:e.employmentType==0?"按日":'按小时',
						time:e.employeeDateTime.workDate,
						guzhu:e.shortWorkerName||'',
						shortWorkerPhone:e.shortWorkerPhone || '',
						location:e.address,
						money:e.salary,
						id:e.id,
						period:e.period,
						status:e.status
					}
				}))
			}
		})
	}
	//取消订单原因
	const getCancelOrderReason = ()=>{
		const par  ={
			pageNo:1,
			pageSize:10
		}
		proxy.$req('GET', proxy.$api.meiyeShortWorkerCancelList,par).then((res) => {
			if(res.code === 200){
				cancelOrderReason.value  = res.result.records
			}
		})
	}
	
	//删除订单
	const delOrder = (e)=>{
		proxy.$req('DELETE', proxy.$api.deleteEmployerOrderByEmp+'/'+e.id).then((res) => {
			if(res.code !== 200){
				uni.$u.toast(res.message)
			}else{
				obj.pageNo = 1;
				detailList.value = []
				getEmployerList()
			}
		})
	}
	//完成验收
	const acceptanceOrder = (e) => {
		const par={
		  "operationType": 3,
		  "orderId": e.id,
		  // "reasonId":cancelOrderReason.value[0].id,//后面这里芳选择的取消原因的id
		  "userTimePeriodId":e.period
		}
		proxy.$req('PUT', proxy.$api.completeAcceptanceOrder,par).then((res) => {
			if(res.code !== 200){
				uni.$u.toast(res.message)
			}else{
				obj.pageNo = 1;
				detailList.value = []
				getEmployerList()
			}
		})
	}
	//支付钱钱
	const paymoney = (e)=>{
		proxy.$req('POST', proxy.$api.shortWorkerPay,{
			"orderId": e.id,
			"orderNo": e.orderno
		}).then((res) => {
			uni.$u.toast(res.message)
			if (res.success) {
				let url = JSON.parse(res.result.result).resp_data.counter_url
				// #ifdef APP-PLUS
				plus.runtime.openURL(url)
				console.log('app');
				// #endif
				// #ifdef H5
				window.open(url)
				console.log('h5');
				// #endif
				//支付成功或者失败的回调缺少
			}
		})
	}
	onShow(()=>{
		obj.pageNo = 1;
		detailList.value = []
		getEmployerList()
		getCancelOrderReason()
	})
	
	onReachBottom(()=>{
		if(obj.total>detailList.value.length){
			obj.pageNo++
			getEmployerList()
		}
		
	})
	
	const routerLink = (e) => {
		router.push({
			name: 'serveOrderDetail',
			params: {
				id:e.id
			}
		})
	}
	
	const showPopup = ref(false)
	const cancelOrder=(e)=>{
		obj.currentObj = e
		showPopup.value = true
	}
	const close = ()=>{
		showPopup.value = false
	}
	const open = ()=>{
		
	}
	const cancelSubmit = ()=>{
		if(obj.reasonId == '' || obj.reasonId == undefined){
			uni.showToast({
				icon: 'none',
				title: '请选择取消原因'
			});
			return
		}
		cancelOrderAjax(obj.currentObj)
	}
	const radioChange = (n) => {
		obj.reasonId = n
		console.log('radioChange', n);
	}
	//取消订单
	const cancelOrderAjax = (e) => {
		const par={
		  "operationType": 4,
		  "orderId": e.id,
		  "reasonId":obj.reasonId,//后面这里芳选择的取消原因的id
		  "userTimePeriodId":e.period
		}
		proxy.$req('PUT', proxy.$api.meiyeShortWordersOrderCancelOrder,par).then((res) => {
			if(res.code !== 200){
				uni.$u.toast(res.message)
			}else{
				obj.pageNo = 1;
				detailList.value = []
				getEmployerList()
				close()
			}
		})
	}
</script>

<template>
	<u-navbar class="navbar"  bgColor="rgba(255, 255, 255, 0)" :placeholder="true" title="" :autoBack="true">
		<template #center>
			<view style="font-weight: 600;font-size: 36rpx;">短时工订单</view>
		</template>
	</u-navbar>
	<view class="main">
		<u-tabs 
		lineColor="#CB645E"
		lineWidth="50rpx"
		:activeStyle="{
		            color: '#CB645E',
		            fontWeight: '600',
					fontSize:'28rpx'
		        }"
		        :inactiveStyle="{
		            color: 'rgba(0,0,0,0.85)',
					fontSize:'28rpx'
		        }"
		itemStyle="padding:10rpx 36rpx; height: 34px;" :list="tabList" @click="tabClick"></u-tabs>
		<!-- 全部 -->
		<view  class="">
			<view class="mainBox" v-for="(e,i) in detailList" :key="i">
				<view class="mianBoxH">
					<view class="headL">
						{{e.title}}
					</view>
					<view class="headR">
						{{e.right}}
					</view>
				</view>
				<view class="mainC">
					<view class="line">
						<view class="lineL">
							雇佣类型
						</view>
						<text class="lineR">{{e.leixing}}</text>
					</view>
					<view class="line">
						<view class="lineL">
							雇佣时间
						</view>
						<text class="lineR">{{e.time}}</text>
					</view>
					<view class="line">
						<view class="lineL">
							短时工
						</view>
						<text class="lineR">{{e.guzhu}}|{{e.shortWorkerPhone}}</text>
					</view>
					<view class="line">
						<view class="lineL">
							工作地点
						</view>
						<text class="lineR">{{e.location}}</text>
					</view>
					<view class="lineEnd">
						<view class="endR">
							<text class="xinzi">薪资</text><text class="yuan">￥</text><text class="num intFont">{{e.money}}</text>
						</view>
					</view>
					<view style="padding-right: 30rpx;">
						<view class="lookOrder">
							<view class="btn text" v-if="e.status==0 || e.status==1" @click="cancelOrder(e)">
								取消订单
							</view>
							
							<view class="btn text" v-if="e.status=='2'" @click="acceptanceOrder(e)">
								完成验收
							</view>
							<view class="btn text" v-if="e.status=='4'" @click="delOrder(e)">
								删除
							</view>
							<view class="text" @click="routerLink(e)">
								查看订单
							</view>
						</view>
					</view>
					
				</view>
			</view>
		</view>
		
	</view>
	
	<u-popup :show="showPopup" :round="10" @close="close" @open="open">
		<view class="popupMain">
			<view class="title">选择取消原因</view>
			<u-radio-group placement="column">
			    <u-radio
					:customStyle="{marginBottom: '18px'}"
					v-for="item in cancelOrderReason"
					:key="item.id"
					:label="item.cancelContent"
					:name="item.id"
					@change="radioChange"
			    >
			    </u-radio>
			</u-radio-group>
			<view class="popupBtn" @click="cancelSubmit">
				<text>确认取消</text>
				<text>服务开始24小时内不可取消</text>
			</view>
		</view>
		
	</u-popup>
	
</template>

<style lang="scss">
	page{
		background-color: #FAFAFA;
	}
	.popupMain{
		padding:20rpx;
		.title{
			padding:40rpx 18rpx;
		}
		.popupBtn{
			display:flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			background:#CB645E;
			width:80%;
			padding:8rpx 0;
			margin:20rpx auto;
			color:#fff;
			border-radius: 16rpx;
			text:nth-child(1){
				font-size:28rpx;
				
			}
			text:nth-child(2){
				font-size:24rpx;
				
			}
			
		}
	}
	.mainBox{
		width: 686rpx;
		height: 580rpx;
		background: #FFFFFF;
		box-shadow: 8rpx 8rpx 32rpx 0rpx rgba(0,0,0,0.04);
		border: 2rpx solid #FFFFFF;
		margin:20rpx auto;
		border-radius: 20rpx;
		padding: 20rpx 10rpx;
		box-sizing: border-box;
		.mainC{
			margin-top: 20rpx;
			padding-bottom: 70rpx;
			.lookOrder{
				float: right;
				// width: 160rpx;
				height: 64rpx;
				background: rgba(255,255,255,0);
				
				
				text-align: center;
				.text{
					border-radius: 12rpx;
					border: 2rpx solid #CB645E;
					display:inline-block;
					width: 160rpx;
					font-size: 28rpx;
					font-weight: 400;
					color: #CB645E;
					line-height: 64rpx;
					
				}
				.text:last-child{
					margin-left:10rpx;
				}
			}
			.lineEnd{
				width: 650rpx;
				height: 100rpx;
				margin: 0 auto;
				padding: 10rpx 20rpx;
				box-sizing: border-box;
				.endR{
					float: right;
					.xinzi{
						display: inline-block;
						font-size: 32rpx;
						font-weight: 600;
						margin-right: 10rpx;
					}
					.yuan{
						font-size: 28rpx;
						font-weight: bold;
						color: #F22F19;
					}
					.num{
						font-size: 44rpx;
						font-weight: bold;
						color: #F22F19;
						line-height: 52rpx;
					}
				}
			}
			.line{
				width: 650rpx;
				height: 64rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;
				.lineL{
					font-size: 28rpx;
					font-weight: 600;
					
				}
				.lineR{
					width: 460rpx;
					color: #3b3b3b;
					font-size: 28rpx;
					font-weight: 400;
					
				}
			}
		}
		
		.mianBoxH{
			height: 80rpx;
			padding: 10rpx 10rpx;
			box-sizing: border-box;
			display: flex;
			margin: 0 auto;
			justify-content: space-between;
			width: 654rpx;
			border-bottom: 1rpx solid rgba(0,0,0,0.05);
			.headL{
				font-size: 32rpx;
				font-weight: 600;
				color: #000000;
			}
			.headR{
				font-size: 28rpx;
				font-weight: 400;
				color: #F22F19;
			}
		}
	}
</style>
